Skip to content

Conversation

@Limitless2023
Copy link

Summary

Fixes #2912 — The SequentialThinkingServer stores all thoughts in memory arrays (thoughtHistory and branches) that grow without bound across the server process lifetime. In long-running sessions (6-8+ hours of frequent use), this can lead to 10GB+ RAM consumption.

Root Cause

thoughtHistory: ThoughtData[] and branches: Record<string, ThoughtData[]> are append-only — thoughts are pushed but never removed or trimmed. Since the server is a long-lived stdio process, memory accumulates indefinitely.

Changes

1. Configurable max history with auto-trimming (lib.ts)

  • Added SEQUENTIAL_THINKING_MAX_HISTORY environment variable (default: 1000)
  • When thought count exceeds the limit, oldest thoughts are trimmed via splice()
  • This bounds memory usage to a predictable maximum regardless of session length

2. Explicit clearHistory() method (lib.ts)

  • New public method that resets both thoughtHistory and branches to empty
  • Returns metadata about what was cleared (previous thought/branch counts)

3. New sequentialthinking_clear tool (index.ts)

  • Registers a tool that clients can invoke to explicitly free memory
  • Useful between distinct thinking sessions within the same server process

4. Documentation (README.md)

  • Added environment variables table documenting both DISABLE_THOUGHT_LOGGING and the new SEQUENTIAL_THINKING_MAX_HISTORY

5. Tests (lib.test.ts)

  • clearHistory — verifies thoughts and branches are fully reset
  • Memory management — verifies history trimming at default limit
  • Environment variable — verifies custom SEQUENTIAL_THINKING_MAX_HISTORY is respected

Testing

All 17 tests pass:

✓ __tests__/lib.test.ts (17 tests) 4ms
Test Files  1 passed (1)
     Tests  17 passed (17)

Design Decisions

  • Default limit of 1000: Conservative — retains substantial context while preventing runaway growth. A single thought is typically a few hundred bytes of text, so 1000 thoughts ≈ a few hundred KB.
  • Trim oldest first: Most recent thoughts are the most relevant for ongoing reasoning. Oldest thoughts are the least likely to be referenced.
  • Separate clear tool: Allows explicit memory management without losing the auto-trim safety net.

…istory

The SequentialThinkingServer stores all thoughts in memory arrays that
grow without bound across the lifetime of the server process. In
long-running sessions (6-8+ hours), this can consume 10GB+ of RAM.

Changes:
- Add configurable max history limit (SEQUENTIAL_THINKING_MAX_HISTORY
  env var, default 1000) with automatic trimming of oldest thoughts
- Add clearHistory() method to explicitly free memory
- Register 'sequentialthinking_clear' tool so clients can reset state
- Add tests for memory management and history clearing

Fixes modelcontextprotocol#2912
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Huge memory occupation by sequentialthinking MCP

2 participants